from stock.base.base_stock_price import BaseStockPrice
from utils.log_util import saveLog
import json


class BaseEastMoneyWorker(BaseStockPrice):

    # 股票板块  行业板块
    # 主力净额    10日：174    5日：164    今日：62
    # 涨跌幅      10日：160    5日：109    今日：3
    industryPlate10Day = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112307794893353689554_1708961760013&fid=f174&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A2&fields=f12%2Cf14%2Cf2%2Cf160%2Cf174%2Cf175%2Cf176%2Cf177%2Cf178%2Cf179%2Cf180%2Cf181%2Cf182%2Cf183%2Cf260%2Cf261%2Cf124%2Cf1%2Cf13"
    industryPlate5Day = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112307794893353689554_1708961760015&fid=f164&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A2&fields=f12%2Cf14%2Cf2%2Cf109%2Cf164%2Cf165%2Cf166%2Cf167%2Cf168%2Cf169%2Cf170%2Cf171%2Cf172%2Cf173%2Cf257%2Cf258%2Cf124%2Cf1%2Cf13"
    industryPlateDay = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112307794893353689554_1708961760013&fid=f62&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A2&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13"

    # 股票板块  概念板块  10日、5日、今日资金流向
    # 主力净额    10日：174    5日：164    今日：62
    # 涨跌幅      10日：160    5日：109    今日：3
    conceptPlate10Day = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112307794893353689554_1708961760015&fid=f174&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A3&fields=f12%2Cf14%2Cf2%2Cf160%2Cf174%2Cf175%2Cf176%2Cf177%2Cf178%2Cf179%2Cf180%2Cf181%2Cf182%2Cf183%2Cf260%2Cf261%2Cf124%2Cf1%2Cf13"
    conceptPlate5Day = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112307794893353689554_1708961760015&fid=f164&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A3&fields=f12%2Cf14%2Cf2%2Cf109%2Cf164%2Cf165%2Cf166%2Cf167%2Cf168%2Cf169%2Cf170%2Cf171%2Cf172%2Cf173%2Cf257%2Cf258%2Cf124%2Cf1%2Cf13"
    conceptPlateDay = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112307794893353689554_1708961760013&fid=f62&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A3&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13"

    # 主力股票
    mainPower = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112308984050446569893_1710227755789&fid=f184&po=1&pz=5000&pn=1&np=1&fltt=2&invt=2&fields=f2%2Cf3%2Cf12%2Cf13%2Cf14%2Cf62%2Cf184%2Cf225%2Cf165%2Cf263%2Cf109%2Cf175%2Cf264%2Cf160%2Cf100%2Cf124%2Cf265%2Cf1&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A13%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A7%2Bf%3A!2%2Cm%3A1%2Bt%3A3%2Bf%3A!2"

    # 板块下股票
    stockForplate = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112308908225739066848_1708965367632&fid=f62&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=b%3A"
    stockPlateEnd = "&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13"

    # 所有股票
    allStockInfo = "https://73.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112408481297401353647_1708553642130&pn=1&pz=10000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1708553642141"

    # 资金流向多的股票
    inMoneBaseurl = "https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112306970746680150377_1708517425203"
    inMoneEndurl = "&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A13%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A7%2Bf%3A!2%2Cm%3A1%2Bt%3A3%2Bf%3A!2&fields="

    def __init__(self):
        super().__init__()

    def getHotStockPlate(self):
        """
        获取热门板块
        以净额排序时，会过滤小于零的数据
        """
        industry10DayList = self.getWeb(self.industryPlate10Day)
        industry5DayList = self.getWeb(self.industryPlate5Day)
        industryDayList = self.getWeb(self.industryPlateDay)

        concept10DayList = self.getWeb(self.conceptPlate10Day)
        concept5DayList = self.getWeb(self.conceptPlate5Day)
        conceptDayList = self.getWeb(self.conceptPlateDay)

        industry10Plate = [
            item for item in self.getStockInfo(industry10DayList, "f174")
            if item[1] != "-" and item[1] > 0
        ]
        industry5Plate = [
            item for item in self.getStockInfo(industry5DayList, "f164")
            if item[1] != "-" and item[1] > 0
        ]
        industryPlate = [
            item for item in self.getStockInfo(industryDayList, "f62")
            if item[1] != "-" and item[1] > 0
        ]
        concept10Plate = [
            item for item in self.getStockInfo(concept10DayList, "f174")
            if item[1] != "-" and item[1] > 0
        ]
        concept5Plate = [
            item for item in self.getStockInfo(concept5DayList, "f164")
            if item[1] != "-" and item[1] > 0
        ]
        conceptPlate = [
            item for item in self.getStockInfo(conceptDayList, "f62")
            if item[1] != "-" and item[1] > 0
        ]
        # industry10Plate.extend(industry5Plate)
        # industry10Plate.extend(industryPlate)

        # concept10Plate.extend(concept5Plate)
        # concept10Plate.extend(conceptPlate)

        industryResult = self.deleteRepeat(industry10Plate)
        conceptResult = self.deleteRepeat(concept10Plate)
        return industryResult, conceptResult

    def getPlateStock(self, stockList):
        """
        板块下股票
        """
        codeList = []
        for item in stockList:
            stockInfo = self.getWeb(self.stockForplate + item[3] +
                                    self.stockPlateEnd)
            if stockInfo is not None and "data" in stockInfo and stockInfo[
                    "data"] is not None:
                for item in stockInfo["data"]["diff"]:
                    if item["f2"] != "-" and item[
                            "f2"] < self.limitMaxPrice and item[
                                "f2"] not in codeList:
                        codeList.append(item["f12"])
        return codeList

    def mainPowerStock(self):
        """
        主力股票
        """
        stockList = []
        obj = self.getWeb(self.mainPower)
        if obj is not None and "data" in obj and obj["data"] is not None:
            for item in obj["data"]["diff"]:
                dayMain = item["f184"]
                fiveMain = item["f165"]
                tenMain = item["f175"]
                nowPrice = item["f2"]
                name = item["f14"]
                if dayMain != "-" and fiveMain != "-" and tenMain != "-":
                    if dayMain > 0 and fiveMain > 0 and tenMain > 0:
                        if nowPrice != "-" and nowPrice < self.limitMaxPrice and "ST" not in name:
                            stockDict = {}
                            stockDict["股票名称"] = name
                            stockDict["股票代码"] = item["f12"]
                            stockDict["所属板块"] = item["f100"]
                            stockDict["最新价"] = nowPrice
                            stockDict["今日主力占比"] = dayMain
                            stockDict["5日主力占比"] = fiveMain
                            stockDict["10日主力占比"] = tenMain

                            stockDict["今日排名"] = item["f225"]
                            stockDict["5日排名"] = item["f263"]
                            stockDict["10日排名"] = item["f264"]
                            stockList.append(stockDict)
        sortedDictList = sorted(stockList, key=lambda x: x['今日排名'])
        return sortedDictList

    def intoMoneyStock(self):
        """
        获取资金流入多的股票
        """

        todayObj = self.getWeb(
            self.inMoneBaseurl + "&fid=f62" + self.inMoneEndurl +
            "f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13"
        )
        today3Obj = self.getWeb(
            self.inMoneBaseurl + "&fid=f267" + self.inMoneEndurl +
            "f12%2Cf14%2Cf2%2Cf127%2Cf267%2Cf268%2Cf269%2Cf270%2Cf271%2Cf272%2Cf273%2Cf274%2Cf275%2Cf276%2Cf257%2Cf258%2Cf124%2Cf1%2Cf13"
        )
        today5Obj = self.getWeb(
            self.inMoneBaseurl + "&fid=f164" + self.inMoneEndurl +
            "f12%2Cf14%2Cf2%2Cf109%2Cf164%2Cf165%2Cf166%2Cf167%2Cf168%2Cf169%2Cf170%2Cf171%2Cf172%2Cf173%2Cf257%2Cf258%2Cf124%2Cf1%2Cf13"
        )
        today10Obj = self.getWeb(
            self.inMoneBaseurl + "&fid=f174" + self.inMoneEndurl +
            "f12%2Cf14%2Cf2%2Cf160%2Cf174%2Cf175%2Cf176%2Cf177%2Cf178%2Cf179%2Cf180%2Cf181%2Cf182%2Cf183%2Cf260%2Cf261%2Cf124%2Cf1%2Cf13"
        )
        todayResult = self.getStockInfo(todayObj, "f62")
        today3Result = self.getStockInfo(today3Obj, "f267")
        today5Result = self.getStockInfo(today5Obj, "f164")
        today10Result = self.getStockInfo(today10Obj, "f174")
        return todayResult, today3Result, today5Result, today10Result

    def getWeb(self, url):
        result = []
        try:
            response = self.request.get(url)
            stringData = response.decode('utf-8')
            value = stringData[stringData.index("(") + 1:-2]
            result = json.loads(value)
        except Exception as e:
            print("获取失败:" + str(e))
            saveLog("获取失败:" + str(e))
        return result

    def getStockInfo(self, obj, moneyValue):
        # 板块名、涨跌幅、净额

        if obj is not None and "data" in obj:
            infoList = []
            for item in obj["data"]["diff"]:
                child = [
                    item["f14"], item[moneyValue], item["f2"], item["f12"]
                ]
                if isinstance(child[1], str) is False:
                    infoList.append(child)
            return sorted(infoList, key=lambda x: x[1], reverse=True)
        return []

    def getAllStockBaseInfo(self):
        """
        获取所有股票基本信息
        """
        stockList = []
        stockInfo = self.getWeb(self.allStockInfo)
        if stockInfo is not None and "data" in stockInfo:
            for item in stockInfo["data"]["diff"]:
                stockList.append(item)
        return stockList

    def deleteRepeat(self, value):
        """
        删除重复的
        """
        unique_subarrays = []
        # 使用集合去除第一个元素重复的子数组
        seen = set()
        for subarr in value:
            if subarr[0] not in seen:
                unique_subarrays.append(subarr)
                seen.add(subarr[0])
        return unique_subarrays
